package com.jader.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class Textnum {
	EditText content;// 定义一个文本输入框

	TextView hasnum;// 用来显示剩余字数

	int num = 10;// 限制的最大字数
	
public static void numtext(final EditText content,final TextView hasnum,final int num ){
	hasnum.setText(num + "");

	content.addTextChangedListener(new TextWatcher() {
		private CharSequence temp;
		private int selectionStart;
		private int selectionEnd;

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			temp = s;
		}

		@Override
		public void afterTextChanged(Editable s) {
			int number = num - s.length();
			hasnum.setText("" + number);
			selectionStart = content.getSelectionStart();
			selectionEnd = content.getSelectionEnd();
			if (temp.length() > num) {
				s.delete(selectionStart - 1, selectionEnd);
				int tempSelection = selectionEnd;
				content.setText(s);
				content.setSelection(tempSelection);// 设置光标在最后
			}
		}

	});
}

}
